package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceimpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断有无商品
      List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);
        if (list!=null&&list.size()!=0)
        {   ShoppingCart cart=list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumberById(cart);
            return;
        }

        //查询出菜品或者套餐的名称以及图片
        Long dishId= shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        //判断是菜品还是套餐
        if (dishId==null)
        {//套餐
        SetmealVO setmealV0 =setmealMapper.getById(setmealId);
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealV0,setmeal);
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        else
        {       //添加的是菜品
             List<Dish> dishes=dishMapper.select(Collections.singletonList(dishId));
             Dish dish=dishes.get(0);
        shoppingCart.setName(dish.getName());
        shoppingCart.setImage(dish.getImage());
        shoppingCart.setAmount(dish.getPrice());}

        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());

        //插入数据
        shoppingCartMapper.add(shoppingCart);

    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> select() {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //先查询出菜品或者套餐
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //获得查询的数据
        shoppingCart=list.get(0);
        shoppingCart.setNumber(shoppingCart.getNumber()-1);

        shoppingCartMapper.sub(shoppingCart);
    }

    @Override
    public void clean() {
        Long id=BaseContext.getCurrentId();
        shoppingCartMapper.clean(id);
    }

}
